import request from '@/utils/request'
import type { AxiosResponse } from 'axios'
import { ElMessage } from 'element-plus'

//定义接口
interface fetchOrderEditType {
  id: number
  orderNo: string
  orderTime: string
  phone: string
  consignee: string
  deliverAddress: string
  deliveryTime: string
  remarks: string
  orderAmount: string
  orderState: string
}
interface LoginResponse {
  code: number
  msg: string
}
interface fetchGoodsListType {
  currentPage: number
  pageSize: number
}
interface fetchGoodsAddType {
  name: string
  category: string
  price: string
  imgUrl: string
  goodsDesc: string
}
interface fetchGoodsDelType {
  id: number
}

interface fetchGoodsEditType {
  name: string
  category: string
  price: string
  imgUrl: string
  goodsDesc: string
  id: number
}
interface fetchGoodsCatelistType {
  currentPage: number
  pageSize: number
}
interface fetchGoodsEditCateType {
  id: number
  cateName: string
  state: boolean
}
interface fetchGoodsCateDelType {
  id: number
}
interface fetchGoodsAddCateType {
  cateName: string
  state: boolean
}
//二次封装，只需要传入参数，提取url以及处理返回的数据
export const fetchOrderEdit = async (data: fetchOrderEditType) => {
  const res: AxiosResponse<LoginResponse> = await request.post('/order/edit', data)

  if (res.data.code === 0) {
    ElMessage({
      type: 'success',
      message: res.data.msg,
    })
  }
  if (res.data.code === 1) {
    ElMessage({
      type: 'warning',
      message: '修改订单失败',
    })
  }
  return res.data
}
//二次封装，只需传入参数
export const fetchGoodsList = async (data: fetchGoodsListType) => {
  const res = await request.get('/goods/list', data)
  return res.data
}
export const fetchGoodsCategories = async () => {
  const res = await request.getCategories('/goods/categories')
  return res.data
}
export const fetchGoodsDel = async (data: fetchGoodsDelType) => {
  const res = await request.get('/goods/del', data)
  return res
}
export const fetchGoodsCatelist = async (data: fetchGoodsCatelistType) => {
  const res = await request.get('/goods/catelist', data)
  return res
}
export const fetchGoodsCateDel = async (data: fetchGoodsCateDelType) => {
  const res = await request.get('/goods/delcate', data)
  return res
}
export const fetchGoodsAdd = async (GoodsData: fetchGoodsAddType) => {
  const res = await request.post('/goods/add', GoodsData)
  return res.data
}
export const fetchGoodsEdit = async (GoodsEditData: fetchGoodsEditType) => {
  const res = await request.post('/goods/edit', GoodsEditData)
  return res.data
}
export const fetchGoodsEditCate = async (GoodsEditCateData: fetchGoodsEditCateType) => {
  const res = await request.post('/goods/editcate', GoodsEditCateData)
  return res.data
}
export const fetchGoodsAddCate = async (GoodsAddCateData: fetchGoodsAddCateType) => {
  const res = await request.post('/goods/addcate', GoodsAddCateData)
  return res.data
}
